package com.entor.controller;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.entor.model.Fileinfo;
import com.entor.model.User;
import com.entor.service.FileInOutService;
import com.entor.util.HttpPostUtil;
import com.entor.util.Msg;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/ic")
public class IndexController {
	
	@Autowired
	private FileInOutService fios;
	
	@RequestMapping("/login")
	public String login() {
		return "/login";
	}
	@RequestMapping("/index")
	public String index() {
		return "/index";
	}
	
	@RequestMapping("/register")
	public String register() {
		return "/register";
	}
	
	@RequestMapping("/toLogin")
	@ResponseBody
	public Msg toLogin(HttpServletRequest req,User u) {
		String path = "user/toLoginByLc";
		String result = HttpPostUtil.doPostRequest(path, u);
		Msg msg = new Msg();
		if(result!=null) {
			msg = JSON.parseObject(result, Msg.class);//把json字符串转换成json对象
			String userStr = JSON.toJSONString(msg.getObj());
			User user = JSON.parseObject(userStr, User.class);
			//获取session
			HttpSession session = req.getSession();
			//存放到session的属性中
			session.setAttribute("user", user);
		}
		return msg;
	}
	
	@RequestMapping("/toValidat")
	@ResponseBody
	public String toValidat(User u) {
		
		ObjectMapper mapper =  new ObjectMapper();
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		
		String path = "user/find";
		String re = HttpPostUtil.doPostRequest(path, u);
		map.put("valid", false);
		if("[]".equals(re)) {
			map.put("valid", true);
		}
		String result = "";
		try {
			result = mapper.writeValueAsString(map);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
	@RequestMapping("/userRegister")
	@ResponseBody
	public Msg userRegister(User u) {
		Msg msg = new Msg();
		String path = "user/add";
		String result = HttpPostUtil.doPostRequest(path, u);
		if(result!=null) {
			msg = JSON.parseObject(result, Msg.class);//把json字符串转换成json对象
		}
		return msg;
	}
	
	
	//读取头像
	@RequestMapping("/showPhoto/{id}")
	public void showPhoto(@PathVariable("id") int id,
			HttpServletResponse rep,HttpServletRequest req){
		try {
			String path = "fileinfo/find";
			Fileinfo fi = new Fileinfo();
			fi.setTid(id);
			fi.setTname("t_urc_user");
			String result = HttpPostUtil.doPostRequest(path, fi);
			if(result!=null) {
				List<Fileinfo> list = JSON.parseArray(result, Fileinfo.class);//把json字符串转换成json集合
				if(list.size()>0){
					path = list.get(0).getPath();
				}else{
					path = "D:/java201807/aaa.jpg";
				}
			}
			byte []b = fios.showPhote(path);
			//设置response的响应到页面的文件类型
			rep.setContentType("image/*");
			//获取输出流
			OutputStream os = rep.getOutputStream();
			//输出到页面
			os.write(b);
			//关闭
			os.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/accunt")
	public String accunt(){
		return "/accunt";
	}
	@RequestMapping("/photo")
	public String photo(){
		return "/user/photo";
	}

}


















